package com.yango.teach.controller;


import com.yango.commonutil.result.Result;
import com.yango.teach.entity.Video;
import com.yango.teach.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程课时 前端控制器
 * </p>
 *
 * @author LiDingYin
 * @since 2021-07-24
 */
@Api(description = "课程课时 前端控制器")
@RestController
@RequestMapping("/teach/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    //添加课时
    @ApiOperation(value = "添加课时")
    @PostMapping("/saveVideo")
    public Result saveVideo(@ApiParam(name = "video", value = "课时对象", required = true)
                            @RequestBody Video video) {
        boolean save = videoService.save(video);
        if (save) {
            return Result.ok().message("添加课时成功!");
        } else {
            return Result.error().message("添加课时失败!");
        }
    }

    //根据id查询课时
    @ApiOperation(value = "根据id查询课时")
    @GetMapping("/getVideoById/{id}")
    public Result getVideoById(@ApiParam(name = "id", value = "课时id", required = true)
                               @PathVariable String id) {
        Video video = videoService.getById(id);
        if (video != null) {
            return Result.ok().data("video", video);
        } else {
            return Result.error().message("没有查询到课时信息!");
        }
    }

    //修改课时
    @ApiOperation(value = "修改课时")
    @PutMapping("/updateVideoById/{id}")
    public Result updateVideoById(@ApiParam(name = "id", value = "课时id", required = true)
                                  @PathVariable String id,
                                  @ApiParam(name = "video", value = "课时对象", required = true)
                                  @RequestBody Video video) {
        video.setId(id);
        boolean update = videoService.updateById(video);
        if (update) {
            return Result.ok().message("修改课时成功!");
        } else {
            return Result.error().message("修改课时失败!");
        }
    }

    //根据id删除课时
    @ApiOperation(value = "根据课时id删除课时")
    @DeleteMapping("/removeById/{id}")
    public Result removeById(@ApiParam(name = "id", value = "课时id", required = true)
                             @PathVariable String id) {
        boolean remove = videoService.removeVideoById(id);
        if (remove) {
            return Result.ok().message("删除课时成功!");
        } else {
            return Result.error().message("删除课时失败!");
        }
    }


}

